<?php

/**
 * ShortText
 *
 * @package Web
 * @author Bruno Pedro <bpedro@computer.org>
 **/
class ShortText
{

	/**
	 * View state of the web form.
	 *
	 * @var string
	 **/
    private $viewState = null;

	/**
	 * Initial URL of the shortText service.
	 *
	 * @var string
	 **/
    private $initialURL = null;

	/**
	 * URL where the script should post text to.
	 *
	 * @var string
	 **/
    private $postURL = null;

	/**
	 * Constructor
	 *
	 * @param $initialURL string The initial URL of the shortText service.
	 * @param $postURL string The script where text should be posted to.
	 * @return void
	 **/
    function __construct ($initialURL = 'http://www.shorttext.com/', $postURL = 'default.aspx') {
        require_once 'HTTP/Request.php';
        $this->initialURL = $initialURL;
        $this->postURL = $initialURL . $postURL;
    }

	/**
	 * Gets the view state from the web form.
	 *
	 * @return mixed The view state or false on error.
	 **/
    private function getViewState () {
        //
        // Prepare a new HTTP request.
        //
        $req =& new HTTP_Request($this->initialURL);
        
        if (!PEAR::isError($req->sendRequest())) {
            //
            // Get the response.
            //
            $res = $req->getResponseBody();
            
            //
            // Get the viewstate from the HTML output.
            //
            $found = preg_match('/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/i', $res, $matches);
            if ($found) {
                $this->viewState = $matches[1];
                return $this->viewState;
            }
        }
        return false;
    }

	/**
	 * Posts text to the shortText service.
	 *
	 * @param $body string The text to post.
	 * @return mixed The shortText ID or false on error.
	 **/
    public function post ($body) {
		//
		// Prepare a new HTTP request.
		//
        $req =& new HTTP_Request($this->postURL);
        $req->setMethod(HTTP_REQUEST_METHOD_POST);

		//
		// Set the POST variables.
		//
        $req->addPostData('btnUrl', 'Create URL');
        $req->addPostData('__VIEWSTATE', $this->getViewState());
        $req->addPostData('txtMaal', base64_encode($body));

        if (!PEAR::isError($req->sendRequest())) {
            //
            // Get the response.
            //
            $res = $req->getResponseBody();

			//
			// Get the ID from within the HTML output.
			//
			$pattern = '/<span id="lblURL" class="mainURL"><a href=\'(.*?)\'>/i';
            $found = preg_match($pattern, $res, $matches);
            if ($found) {
                $contentURL = $matches[1];
				$pattern = '/' . addcslashes($this->initialURL, '/.') . '(.*?)$/i';
                $found = preg_match($pattern, $contentURL, $matches);
                if ($found) {
                    //
                    // Return the shortText ID.
                    //
                    return $matches[1];
                }
            }
        }
        return false;
    }

	/**
	 * Gets text from the shortText service.
	 *
	 * @param $id string The shortText ID.
	 * @return mixed The text or false on error.
	 **/
    public function get ($id) {
        //
        // Prepare a new HTTP request.
        //
        $req =& new HTTP_Request($this->initialURL . $id);
        
        if (!PEAR::isError($req->sendRequest())) {
            //
            // Get the response.
            //
            $res = $req->getResponseBody();
            
            //
            // Get the text body from the HTML output.
            //
			$pattern = '/<span id="lblData" class="bodyMaal"><font color="Black">(.*?)<\/font><\/span>/i';
            $found = preg_match($pattern, $res, $matches);
            if ($found) {
                //
                // Return the blog body. It needs to be decoded
                // because we encode it when publishing.
                //
                return base64_decode($matches[1]);
            }
        }
        return false;
    }
}

?>
